package openadmin.dao;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;
import java.util.Vector;

import openadmin.dao.exception.DataException;
import openadmin.jaas.Context;
import openadmin.model.control.ActionClass;
import openadmin.model.control.User;
import openadmin.widgets.swing.TemplateJFormatTxt01;
import openadmin.widgets.swing.TemplateLabel01;


public class TestDao {

	/**
	 * @param args
	 */
	
	protected TemplateLabel01[] label = new TemplateLabel01[5];
	private Vector<TemplateLabel01> labelSet = new Vector<TemplateLabel01>();
	private Vector<Object> objecteSet = new Vector<Object>();
	protected Object[] objecte = new Object[25];
	private ActionClass classe;
	private Base object;
	private Context ctx;
	
	/**
	public TestDao (Context ctx, Base obj){
	
		this.ctx = ctx;
		
		try {
			
			this.classe = Class.forName(obj.getClass().getName());
			this.object = obj;
		
		}catch (ClassNotFoundException e) {
				
			e.printStackTrace();
		
		}
		
		
	}
	
	public static void main(String[] args) {
		
		Base sec = new Class();
		//sec.setNomseccio("Alfred");
		TestDao ts = new TestDao(null, sec);
		ts.metodeSet("alta");
		//ts.test(sec);
		//ts.altaObjecte();

	}

	public void test(Base obj){
		
		
		try {
		
			classe = Class.forName(obj.getClass().getName());
			object = obj;
		
		} catch (ClassNotFoundException e) {
				
			e.printStackTrace();
		}
		
		
		System.out.println("initiation Annotattion");
		for (Field f : obj.getClass().getDeclaredFields()) {
			
			if (f.isAnnotationPresent(FieldProperties.class) 
				&& ((f.getAnnotation(FieldProperties.class).TypeField()) == TypesComponents.id 
				|| (f.getAnnotation(FieldProperties.class).TypeField()) == TypesComponents.lit
				|| (f.getAnnotation(FieldProperties.class).TypeField()) == TypesComponents.obj)){
				
				//crear label i text
				label[(f.getAnnotation(FieldProperties.class).nia())]=(new TemplateLbl01("f.getName()"));
				labelSet.insertElementAt(new TemplateLbl01("f.getName()"), (f.getAnnotation(FieldProperties.class).nia()));
				objecte[(f.getAnnotation(FieldProperties.class).nia())]= 
					new TemplateJFTxt01(f.getAnnotation(FieldProperties.class).size(), 
										f.getAnnotation(FieldProperties.class).Visible(), 
										f.getAnnotation(FieldProperties.class).Editable());
				objecteSet.add(new TemplateJFTxt01(f.getAnnotation(FieldProperties.class).size(), 
						f.getAnnotation(FieldProperties.class).Visible(), 
						f.getAnnotation(FieldProperties.class).Editable()));
				
				System.out.println("Annotation: " + f.getName());
				
				System.out.println("\tnia: " + f.getAnnotation(FieldProperties.class).nia());
				System.out.println("\tEditable: " + f.getAnnotation(FieldProperties.class).Editable());
				System.out.println("\tVisible: " + f.getAnnotation(FieldProperties.class).Visible());
			}
			
		}
		System.out.println("finalize Annotattion");
	}
	
	public void altaObjecte(){
				
		
				
	}
	
	public Base metodeSet(String operacio){
				
		String nomMetode = null;						
		Class[] parametresMetode = null;
		Method metode;
		Base base;				
		Object objecteMetode = null;
		
		
		for (Field f : object.getClass().getDeclaredFields()) {
			
			nomMetode = Character.toUpperCase(f.getName().charAt(0)) + f.getName().substring(1);						
			objecteMetode = null;
			
			if ((!f.isAnnotationPresent(FieldProperties.class))
					|| ((operacio.equals("alta"))
					&& ((f.getAnnotation(FieldProperties.class).TypeField()) == TypesComponents.id)))				
				continue;
			
			else if ((f.isAnnotationPresent(FieldProperties.class)) 					
					&& ((f.getAnnotation(FieldProperties.class).TypeField()) == TypesComponents.id)){
				
				//Falta introduir variable
				objecteMetode = new Integer(1);
				
			}
			
			else if ((f.isAnnotationPresent(FieldProperties.class)) 					
					&& ((f.getAnnotation(FieldProperties.class).TypeField()) == TypesComponents.obj)){
								
				try {
					
					if (ctx != null){
					base = (Base)f.getType().newInstance();
					//falta introduir la variable
					base.setDescription("");
					ctx.getCon().begin();
					//falta controlar si l'objecte es null
					objecteMetode = ctx.getCon().cercaObjectes(base).get(0);
					ctx.getCon().commit();
					}															
					
				} catch (ExcepcioDades e) {
					
					e.printStackTrace();
				
				} catch (InstantiationException e) {
					
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					
					e.printStackTrace();
				}				
				
			}						
			
			else if ((f.isAnnotationPresent(FieldProperties.class)) 					
					&& ((f.getAnnotation(FieldProperties.class).TypeField()) == TypesComponents.lit)){
				
				//Falta introduir variable
				objecteMetode = new String ("Maite");
				
			}
			
			else if ((f.isAnnotationPresent(FieldProperties.class)) 					
					&& ((f.getAnnotation(FieldProperties.class).TypeField()) == TypesComponents.chk)){
				
				//Falta introduir variable
				objecteMetode = new Boolean(true);
				
			}
			
			else return null;
			
			nomMetode = "set" + nomMetode;			
			parametresMetode = new Class[] {f.getType()};
			
			try {
				
				metode = object.getClass().getMethod(nomMetode, parametresMetode);				
				metode.invoke(object, objecteMetode);							
				
			}catch (IllegalArgumentException e) {
			
				e.printStackTrace();
			
			} catch (IllegalAccessException e) {
					
				e.printStackTrace();
			
			} catch (InvocationTargetException e) {
					
				e.printStackTrace();
				
				
			} catch (SecurityException e) {
				
				e.printStackTrace();
			} catch (NoSuchMethodException e) {
				
				e.printStackTrace();
			}
			
			
			System.out.println(nomMetode);
			System.out.println(parametresMetode.getClass().getCanonicalName());
			
		}
		return null;
		
	}
	
	public void DesignsForm(){
		
		
	}
	
	public void altaSeccion(){
		
		Class sec = new Class();
		
		sec.setDescription("xxx");
		
		
	}*/
}
